package com.dm.shop.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dm.shop.common.enums.ResultEnum;
import com.dm.shop.dto.AuthAdminDTO;
import com.dm.shop.entity.Admin;
import com.dm.shop.service.AdminService;
import com.dm.shop.utils.ResultUtil;
import com.dm.shop.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MyBatisPlusGenerater-Saiya Wang
 * @since 2021-07-20
 */
@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    AdminService adminService;

//    //用户密码加密接口
//    @Resource
//    PasswordEncoder passwordEncoder;

    @PostMapping(value = "/login")
    public Result login(@RequestBody @Valid AuthAdminDTO authAdminDTO)
    {
        //条件构造器
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        //查询电话为获取的username的 管理员
        wrapper.eq("phone_number",authAdminDTO.getUsername());
        System.out.println("这里是输出"+wrapper);
        //通过wrapper找管理员
        Admin admin = adminService.getOne(wrapper);
        //
//        if(admin == null || passwordEncoder.matches(authAdminDTO.getPassword(),admin.getPassword()))
        if(admin == null || (!admin.getPassword().equals(authAdminDTO.getPassword())))
        {
            return ResultUtil.error(ResultEnum.LOGIN_FAILED);
        }
        return ResultUtil.success("登录成功",admin);
    }

    @GetMapping("")
    public String test()
    {
        return "你好，管理员！";
    }
}
